package com.weixin.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.weixin.entity.Supplier;
import com.weixin.service.ISupplierService;
import com.weixin.service.impl.SupplierServiceImpl;
import com.weixin.util.PageSizeUtil;


@WebServlet("/SupplierServlet/*")
public class SupplierServlet extends HttpServlet {
	ISupplierService supplierService=new SupplierServiceImpl();
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path = request.getPathInfo().substring(1);
		if("listSupplier".equals(path)) {
			ListSupplier(request,response);
		}else if("addSupplier".equals(path)) {
			AddSupplier(request,response);
		}else if("preEditSupplier".equals(path)) {
			PreEditSupplier(request,response);
		}else if("editSupplier".equals(path)) {
			EditSupplier(request,response);
		}else if("moveSupplier".equals(path)) {
			MoveSupplier(request,response);
		}else if("sarchInfo".equals(path)) {
			SarchInfo(request,response);
		}
		
	}
	private void SarchInfo(HttpServletRequest request, HttpServletResponse response) {
		String search = request.getParameter("search");
		try {
			List<Supplier> supplierList =supplierService.getForSearch(search);
			request.setAttribute("supplierList",supplierList);
			request.setAttribute("pageCount",1);
			request.setAttribute("pageNum",1);
			request.getRequestDispatcher("/page/user/supplier/listSupplier.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	private void MoveSupplier(HttpServletRequest request, HttpServletResponse response) {
		int supplierId=Integer.valueOf(request.getParameter("supplier_id"));
		try {
			if(supplierService.moveById(supplierId)){
				response.getWriter().print(1);
			}else{
				response.getWriter().print(0);
			}
		} catch (Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	private void EditSupplier(HttpServletRequest request, HttpServletResponse response) {
		int supplierId=Integer.valueOf(request.getParameter("supplier_id"));
		String name=request.getParameter("supplier_name");
		String address=request.getParameter("supplier_address");
		String supplier_=request.getParameter("supplier_");//供应商公司名
		int telephone = Integer.valueOf(request.getParameter("telephone"));
		String main_business = request.getParameter("main_business");
		String remark=request.getParameter("remark");
		try {
			Supplier supplier = supplierService.selectById(supplierId);
			supplier.setSupplier_name(name);
			supplier.setSupplier_address(address);
			supplier.setSupplier_(supplier_);
			supplier.setTelephone(telephone);
			supplier.setMain_business(main_business);
			supplier.setRemark(remark);
			
			if(supplierService.editSupplier(supplier)) {
				response.getWriter().print(1);
			}else {
				response.getWriter().print(0);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	private void PreEditSupplier(HttpServletRequest request, HttpServletResponse response) {
		int supplierId=Integer.valueOf(request.getParameter("supplier_id"));
		try {
			Supplier supplier = supplierService.selectById(supplierId);
			request.setAttribute("supplier",supplier);
			request.getRequestDispatcher("/page/user/supplier/editSupplier.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void AddSupplier(HttpServletRequest request, HttpServletResponse response) {
		String name=request.getParameter("supplier_name");
		String address=request.getParameter("supplier_address");
		String supplier_=request.getParameter("supplier_");//供应商公司名
		int telephone = Integer.valueOf(request.getParameter("telephone"));
		String main_business = request.getParameter("main_business");
		String remark=request.getParameter("remark");
		
		Supplier supplier=new Supplier();
		supplier.setSupplier_name(name);
		supplier.setSupplier_address(address);
		supplier.setSupplier_(supplier_);
		supplier.setTelephone(telephone);
		supplier.setMain_business(main_business);
		supplier.setRemark(remark);
		System.out.println(supplier);
		try {
			if(supplierService.addSupplier(supplier)) {
				request.setAttribute("addMasg", "添加成功！");
				request.getRequestDispatcher("/page/user/supplier/addSupplier.jsp").forward(request, response);
			}else {
				request.setAttribute("addMasg","添加失败！");
				request.getRequestDispatcher("/page/user/supplier/addSupplier.jsp").forward(request, response);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void ListSupplier(HttpServletRequest request, HttpServletResponse response) {
		int pageNum =Integer.valueOf(request.getParameter("pageNum"));
		try {
			//供应商信息
			List<Supplier> supplierList = supplierService.selectAllByPagination(PageSizeUtil.PAGE_SIZE7, pageNum);
			request.setAttribute("supplierList",supplierList);
			System.out.println(supplierList);
			System.out.println("总数据："+supplierService.getAllTotal());
			//分页条数据
			Double pageCount = Math.ceil(supplierService.getAllTotal()*1.0/PageSizeUtil.PAGE_SIZE7);
			request.setAttribute("pageCount",pageCount);
			request.setAttribute("pageNum",pageNum);
			request.getRequestDispatcher("/page/user/supplier/listSupplier.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
